package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.ProblemVo;
import com.data.core.dao.ProblemMapper;
import com.data.core.entry.Problem;
import com.data.core.service.ProblemService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @version: V1.0
* @author: szw
* @className: ProblemServiceImpl
* @packageName: ProblemServiceImpl
* @description: 问题接口实现
* @data: 2021-1-21
**/  
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem> implements ProblemService {

    @Resource
    private ProblemMapper problemMapper;

    /**
     * 保存问题接口实现
     * @param problemVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-保存问题接口实现",operationType=BaseEnum.INSERT)
    public int save(ProblemVo problemVo) throws Exception {
        if (Objects.isNull(problemVo)){
            throw new BaseException(BaseEnum.PROBLEM_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(problemVo,true);
        }catch (Exception e){
            throw new BaseException(BaseEnum.SAVE_PROBLEM_DATA_IS_ERROR);
        }
    }

    /**
     * 更新问题接口实现
     * @param problemVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新问题接口实现",operationType=BaseEnum.UPDATE)
    public int update(ProblemVo problemVo) throws Exception {
        if (Objects.isNull(problemVo)){
            throw new BaseException(BaseEnum.PROBLEM_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(problemVo,false);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_PROBLEM_DATA_IS_ERROR);
        }
    }

    /**
     * 查询问题接口实现
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询问题接口实现",operationType=BaseEnum.SELECT)
    public Problem findProblemById(Long id) throws Exception {
        if (null==id){
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return problemMapper.selectById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_PROBLEM_DATA_IS_ERROR);
        }
    }

    /**
     * 删除问题接口实现
     * @param id
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-删除问题接口实现",operationType=BaseEnum.DELETE)
    public int delete(Long id) throws Exception {
        if (null==id){
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return problemMapper.deleteById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.DELETE_PROBLEM_DATA_IS_ERROR);
        }
    }

    /**
     * 查询问题列表接口实现
     * @param page
     * @param problemVo
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询问题列表接口实现",operationType=BaseEnum.SELECT)
    public IPage<Problem> problemPage(Page<Problem> page, ProblemVo problemVo) throws Exception {
        if (Objects.isNull(problemVo)){
            throw new BaseException(BaseEnum.PROBLEM_DATA_IS_NULL);
        }
        try {
            QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
            return  problemMapper.selectPage(page,queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_PROBLEM_DATA_IS_ERROR);
        }
    }

    /**
     * 更新、保存问题
     * @param problemVo
     * @param flag true 保存 false 更新
     * @return
     */
    private int saveToUpdate(ProblemVo problemVo,Boolean flag){
        Problem problem = new Problem();
        BeanUtils.copyProperties(problemVo,problem);
        if (flag){
            return problemMapper.insert(problem);
        }
        return problemMapper.updateById(problem);
    }

}
